programming4us
           
 
 
Programming

Programming Excel with VBA and .NET : Tasks in Visual Basic - Check Results

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/12/2011 9:10:07 AM
In many situations, you need to check the result of an operation to tell if it succeeded. The result-checking functions in Visual Basic let you test results before you take actions that might otherwise cause an error. Table 1 lists the result-checking functions.
Table 1. Visual Basic result-checking functions
CategoryFunctionUse to
Boolean testsIsArrayTell if a variable is an array
 IsDateTell if a variable contains data that can be converted to a Date
 IsEmptyTell if a variable has not yet been initialized
 IsErrorTell if a variable contains an Error object
 IsMissingTell if a ParamArray argument was omitted
 IsNullTell if a variable contains no valid data
 IsNumericTell if a variable contains a value that can be converted to a number
 IsObjectTell if a variable is a reference to a valid object
Type testsTypeNameGet the name of the variable's type as a string
 TypeOfTest the type of a variable within an If block
 VarTypeGet the variable's type as a VbVarType constant

Most of these tests are used with variables that were declared as Variant or Object data types. Those types of variables can contain many different kinds of data, so it is often necessary to test what the variable contains before proceeding in code.

There are several common uses of this in Excel. The first is ActiveSheet.property, which may refer to a Worksheet, Chart, or other object:

    Sub ChangeSheets( )
Select Case TypeName(ActiveSheet)
Case "Worksheet"
If ActiveSheet.Index < Worksheets.Count Then
Worksheets(ActiveSheet.Index + 1).Activate
Else
Worksheets(1).Activate
End If
Case "Chart"
If ActiveSheet.Index < Charts.Count Then
Charts(ActiveSheet.Index + 1).Activate
Else
Charts(1).Activate
End If
Case Else
Debug.Print TypeName(ActiveSheet), ActiveSheet.Name
End Select
End Sub

The preceding code uses a Select statement to perform different actions based on the TypeName of the active sheet. You usually combine TypeName with SelectTypeName is also handy for checking whether or not an optional argument has been omitted: when there are more than two possibilities as shown in the preceding block.

    Public Sub Reformat(Optional ws As Worksheet)
' Check if argument was omitted.
If TypeName(ws) = "Nothing" Then
' Check the type of the active sheet.
If TypeName(ActiveSheet) = "Worksheet" Then
' Format the active worksheet.
Set ws = ActiveSheet
Else
' You can't reformat nonworksheets.
MsgBox "Select a worksheet and try again."
Exit Sub
End If
End If
Dim rng As Range
' Get the cells with data in them.
Set rng = ws.UsedRange
' Apply AutoFormat
rng.AutoFormat xlRangeAutoFormatSimple
End Sub

In the preceding code, the choices are either/or: if the argument is omitted, check the active sheet; if that sheet is a worksheet, use it. Alternately, you can use the TypeOf keyword within an If statement; however, TypeOf can't test if the variable is Nothing. To do that, you need to use either TypeName or the Is operator, as shown by this different version of the preceding code:

    Public Sub Reformat2(Optional ws As Worksheet)
' Check if argument was omitted.
If ws Is Nothing Then
' Check the type of the active sheet.
If TypeOf ActiveSheet Is Worksheet Then
' Format the active worksheet.
Set ws = ActiveSheet
Else
' You can't reformat nonworksheets.
MsgBox "Select a worksheet and try again."
Exit Sub
End If
End If
Dim rng As Range
' Get the cells with data in them.
Set rng = ws.UsedRange
' Apply AutoFormat
rng.AutoFormat xlRangeAutoFormatSimple
End Sub

Reformat and Reformat2 are equivalent. I tend to use the TypeName test rather than TypeOf or Is because it lets me use a consistent test for all types of objects, but that's really just a personal preference.

The IsNumeric and IsDate functions are useful when receiving data from a user. Rather than returning specific information about the type of the variable, they let you know if the data in the variable can be converted to those types. For instance, the following code checks the value entered in an InputBox to determine the type of entry the user made:

    Sub CheckEntry( )
Dim var As String, msg As String
var = InputBox("Enter a number, word, or date.")
' Use Boolean tests to check an entry.
If IsNumeric(var) Then
msg = "number."
ElseIf IsDate(var) Then
msg = "date."
ElseIf var = "" Then
msg = "empty."
Else
msg = "string."
End If
Debug.Print "Entry is a " & msg
End Sub

IsNumeric and IsDate are a good way to check values before calling conversion functions like CDate or CDouble.

Other -----------------
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Read and Write Files
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Get Dates and Times
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Work with Text
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 2)
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 1) - Physical Constraints
- Parallel Programming with Microsoft Visual Studio 2010 : Task Parallelism - Unhandled Exceptions in Tasks
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Tasks
- jQuery 1.3 : DOM Manipulation - Moving elements
- .NET Debugging : Introduction to the Tools - .NET 2.0—Redistributable & .NET 2.0—SDK
- .NET Debugging : Managed Heap and Garbage Collection
- Context and Interception : Custom Component Services (part 3) - The Transaction Management Service
- Context and Interception : Custom Component Services (part 2) - The Logbook Service
- Context and Interception : Custom Component Services (part 1) - Building a Custom Context Attribute & Installing a Custom Message Sink
- Software Testing with Visual Studio Team System 2008 : Data-driven unit testing
- Software Testing with Visual Studio Team System 2008 : Unit testing an ASP.NET application
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Replacing an Exception & Logging an Exception
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Diving in with a Simple Example
- iPhone Programming : Connecting to the Network - Embedding a Web Browser in Your App
- iPhone Programming : Connecting to the Network - Detecting Network Status
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Programming - Software Patterns
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us